var bgColor;

$(document).ready(function(){
	$.ajaxSetup ({ cache: false });
	
	$("p").each(function(){display_with_linebreaks($(this));});

	$("#public_status").find("BUTTON").bind("click", function(e){ change_public_status(e) });	
	
	activate_tabs();
	
	$("#default_status").find("BUTTON").bind("click", function(e){ set_resume_as_default(e) });
	
	$("#delete_resume").find("BUTTON").bind("click", function(e){ delete_resume(e) });
	
	//$(".parts_table").find("tr").filter(":even").css("background-color","#dedeff");

});

function activate_tabs()
{
	var all_tab_buttons = $(".tab_button");
	var all_tab_items = $(".tab_item");
	
	all_tab_items.hide();
	all_tab_items.filter(":first").show();
	
	all_tab_buttons.removeClass("active_tab_button").addClass("inactive_tab_button");
	all_tab_buttons.filter(":first").removeClass("inactive_tab_button").addClass("active_tab_button");

	$(".tab_button").bind("click", 
		function(e){
			var tab_button = $(e.target);
			var tab_item_id = tab_button.attr('id').split('.')[0];			// resume_parts in resume_parts.tab_button
			var tab_item = $(".tab_item").filter("#"+tab_item_id).eq(0);
			
			all_tab_items.hide();	// hide all the tab items
			tab_item.show();		// show the asked tab item
			
			all_tab_buttons.removeClass("active_tab_button").addClass("inactive_tab_button");
			tab_button.removeClass("inactive_tab_button").addClass("active_tab_button");
		}
	);

	var bgColor = $(".tab_bar").css("background-color");
	$(".tab_button").hover(
			function(){ 
				if ($(this).hasClass("active_tab_button")){ return; }			// Do NOT highlight the active tab button.
				$(this).css({"background-color":"#dedeff", "cursor":"default"});	
			},
			function(){ 
				$(this).css({"background-color":bgColor, "cursor":"auto"});
			}
	);
	
}

function change_public_status(e)
{
	var resume_id, authToken;
	var postUrl, postData = new Object();
	
	authToken = $("*[name='authenticity_token']").eq(0).val();
	resume_id = $(e.target).parents('.container').eq(0).attr('id').split('_')[1];	// the container has id - resumeId_<id>
	postUrl = '/toggle_public_status/'+resume_id;
	postData["authenticity_token"] = authToken;
	postData["ajax"] = "true";
	postData["request"] = "toggle";
	
	$.post(
		postUrl,
		postData,
		function(response){
			change_public_status_callback(e, response);
		});
}

function change_public_status_callback(e, response)
{
	var responseObj = JSON.parse(response);
	
	if (responseObj["retVal"] == "error"){
		alert("Error");
	}
	
	$(e.target).parents("#public_status").find(".public_status_text").html(responseObj["public_status"]);		
}

function set_resume_as_default(e)
{
	var resume_id;
	var postUrl, postData = new Object();

	resume_id = $(e.target).parents('.container').eq(0).attr('id').split('_')[1];	// the container has id - resumeId_<id>	
	postUrl = '/set_default_resume';
	postData["authenticity_token"] = $("*[name='authenticity_token']").eq(0).val();
	postData["ajax"] = "true";
	postData["resume_id"] = resume_id;
	
	$.post(
		postUrl,
		postData,
		function(response){
			set_as_default_callback(e, response);
		});
}

function set_as_default_callback(e, response)
{
	var responseObj = JSON.parse(response);
	
	if (responseObj["retVal"] != "set"){
		alert(responseObj["retVal"]);
	}	
	$(e.target).parents("#default_status").find(".default_status_text").filter(".hidden").eq(0).removeClass("hidden");
	$(e.target).parents(".default_status_text").eq(0).addClass("hidden");
	
}

function delete_resume(e)
{
	if (!confirm("Sure?"))	{	return	}

	var title = window.location.pathname.split("/")[2];			// third part on splitting /show/:title/rtemplates at '/'
	var postUrl = '/destroy/'+title;
	var postData = new Object();

	postData["authenticity_token"] = $("*[name='authenticity_token']").eq(0).val();
	postData["ajax"] = "true";
	
	$.post(
		postUrl,
		postData,
		function(response){
			var responseObj = JSON.parse(response);
			if (responseObj["retVal"] != "deleted"){
					alert(responseObj["retVal"]);
			}
			window.location.replace("/home");	// redirect to the  user's home page, replace done so this page doesn't show up in history
		});
}